import { VercelRequest, VercelResponse } from "@vercel/node";
import { FeiShuTable, getTableRecords } from "../feishu/bitable";
import { buildURLData } from "web-utility";

export default async function handler(req: VercelRequest, res: VercelResponse) {
    if (req.method == "POST") {
        const { username, password } = req.body;

        const urlParam = `${buildURLData({
            filter: `CurrentValue.[username]="${username}"`
        })}`

        const data = (await getTableRecords(FeiShuTable.User, urlParam))[0];

        if (!data || password != data.password) return res.status(400).json(
            { messsage: "input error, please check" }
        ).end();
        
        return res.status(200).json({
            message: "login success",
            data
        });
    }

    return res.status(200).json({
        message: "executed success",
    });
}
